//
// Created by tonystark on 16/3/22.
// Copyright (c) 2016 sewm. All rights reserved.
//

#import "LoginController.h"
#import "YouDaoNavigationController.h"
#import "Api.h"
#import "Api+Login.h"
#import "LoginInfo.h"
#import "SSKModel+JSONSerializing.h"
#import "SystemManager.h"
#import "FindPwdController.h"

@interface LoginController () {

}
@property(nonatomic, retain) TPKeyboardAvoidingScrollView *scrollView;
@property(nonatomic, retain) UITextField *accountField;
@property(nonatomic, retain) UITextField *pwdField;
@end

@implementation LoginController {

}
- (void)dealloc {
    self.scrollView = nil;
    self.accountField = nil;
    self.pwdField = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.scrollView = [[TPKeyboardAvoidingScrollView alloc] init];
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login-bg"]];
    [self.scrollView addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    //Logo
    //账号
    UIImageView *accountImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login-account"]];
    [self.scrollView addSubview:accountImageView];
    [accountImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).mas_offset(28);
        make.top.mas_equalTo(240);
        make.width.mas_equalTo(36);
        make.height.mas_equalTo(36);
    }];
    self.accountField = [[UITextField alloc] init];
    self.accountField.placeholder = @"手机号/账号";
    self.accountField.text = @"11111111111";
    [self.scrollView addSubview:self.accountField];
    [self.accountField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(accountImageView.mas_right).mas_offset(10);
        make.right.equalTo(self.view).mas_offset(-28);
        make.top.mas_equalTo(240);
        make.height.mas_equalTo(32);
    }];
    //A8C7F1
    UIView *lineView = [UIView new];
    lineView.backgroundColor = UIColorFromRGB(0xA8C7F1);
    [self.scrollView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.accountField);
        make.right.equalTo(self.accountField);
        make.height.mas_equalTo(0.5);
        make.top.equalTo(self.accountField.mas_bottom);
    }];

    //密码
    UIImageView *pwdImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login-pwd"]];
    [self.scrollView addSubview:pwdImageView];
    [pwdImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(accountImageView);
        make.top.equalTo(accountImageView.mas_bottom).mas_offset(30);
        make.size.equalTo(accountImageView);
    }];

    self.pwdField = [[UITextField alloc] init];
    self.pwdField.placeholder = @"密码";
    self.pwdField.secureTextEntry = YES;
    self.pwdField.text = @"kd_pass";
    [self.scrollView addSubview:self.pwdField];
    [self.pwdField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.accountField);
        make.right.equalTo(self.accountField);
        make.top.equalTo(pwdImageView.mas_top);
        make.height.equalTo(self.accountField);
    }];
    UIView *lineView2 = [UIView new];
    lineView2.backgroundColor = UIColorFromRGB(0xA8C7F1);
    [self.scrollView addSubview:lineView2];
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.pwdField);
        make.right.equalTo(self.pwdField);
        make.height.mas_equalTo(0.5);
        make.top.equalTo(self.pwdField.mas_bottom);
    }];
    //登陆
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image = [[UIImage imageNamed:@"login-btn-bg"] getImageResize];
    [loginBtn setBackgroundImage:image forState:UIControlStateNormal];
    [loginBtn setTitle:@"登陆" forState:UIControlStateNormal];
    [self.scrollView addSubview:loginBtn];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).mas_equalTo(28);
        make.right.equalTo(self.view).mas_equalTo(-28);
        make.top.equalTo(pwdImageView.mas_bottom).mas_offset(38);
        make.height.mas_equalTo(44);
    }];
    [loginBtn addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];


    UIButton *findPwdBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [findPwdBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
    [findPwdBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.scrollView addSubview:findPwdBtn];
    [findPwdBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).mas_equalTo(28);
        make.right.equalTo(self.view).mas_equalTo(-28);
        make.top.equalTo(loginBtn.mas_bottom).mas_offset(30);
        make.height.mas_equalTo(20);
    }];
    [findPwdBtn addTarget:self action:@selector(findPwdAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (UIView *)topView {
    return nil;
}

#pragma mark - Status Bar

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (BOOL)prefersStatusBarHidden {
    return NO;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationNone;
}

#pragma mark action

- (void)loginAction:(UIButton *)sender {
    if (self.accountField.text.length == 0) {
        [self.view showFaildText:nil detailText:@"请输入账号"];
        return;
    }
    if (self.pwdField.text.length == 0) {
        [self.view showFaildText:nil detailText:@"请输入密码"];
        return;
    }
    WeakSelf
    [self.view showLoadingView:nil detailText:@"登录中..."];
    [Api login:@{@"mobile" : self.accountField.text, @"password" : self.pwdField.text} success:^(id body) {
        [weakSelf.view dismissLoadingView];
        [[SystemManager sharedInstance] configLoginInfo:[LoginInfo modelFromJSONDictionary:body]];
        [[YouDaoNavigationController sharedInstance] toggleTabBar];
    }    faild:^(NSString *message, NSInteger code) {
        [weakSelf.view showFaildText:nil detailText:message];
    }];
}

- (void)findPwdAction:(id)sender {
    FindPwdController *controller = [[FindPwdController alloc] init];
    [self pushController:controller];
}

@end